package menuimpl

import (
	"context"
	"go-caipu/pkg/services/admin/menu"
	"gorm.io/gorm"
)

type Service struct {
	store store
}

func ProvideService(db *gorm.DB) (*Service, error) {
	store := ProvideStore(db)
	s := &Service{
		store: store,
	}
	return s, nil
}

func (s *Service) GetMenuList(ctx context.Context) (data []menu.Menu, err error) {
	var result = make([]menu.Menu, 0)
	result, err = s.store.GetMenuList(ctx)
	for i := 0; i < len(result); i++ {
		if result[i].ParentId != 0 {
			continue
		}
		menusInfo := makeMenuTree(&result, result[i])
		data = append(data, menusInfo)
	}
	return
}
func (s *Service) MenuTreeSelect(ctx context.Context) (data []menu.TreeSelect, err error) {
	var result = make([]menu.Menu, 0)
	result, err = s.store.GetMenuList(ctx)
	for _, item := range result {
		if item.ParentId != 0 {
			continue
		}
		data = append(data, makeMenuTreeSelect(&result, item))
	}
	return data, nil
}
func makeMenuTreeSelect(menuList *[]menu.Menu, current menu.Menu) menu.TreeSelect {
	list := *menuList
	min := make([]menu.TreeSelect, 0)

	item := menu.TreeSelect{
		Key:      current.MenuId,
		ParentId: current.ParentId,
		Title:    current.Title,
		Sort:     current.Sort,
	}

	for j := 0; j < len(list); j++ {
		if item.Key != list[j].ParentId {
			continue
		}
		mi := makeMenuTreeSelect(menuList, list[j])
		if list[j].MenuType != Button {
			ms := makeMenuTreeSelect(menuList, list[j])
			min = append(min, ms)
		} else {
			min = append(min, mi)
		}
	}
	item.Children = min
	return item
}

func makeMenuTree(menuList *[]menu.Menu, current menu.Menu) menu.Menu {
	list := *menuList
	min := make([]menu.Menu, 0)
	for j := 0; j < len(list); j++ {
		if current.MenuId != list[j].ParentId {
			continue
		}
		mi := list[j]
		if mi.MenuType != Button {
			ms := makeMenuTree(menuList, mi)
			min = append(min, ms)
		} else {
			min = append(min, mi)
		}
	}
	current.Children = min
	return current
}

func (s *Service) GetMenu(ctx context.Context, cmd menu.GetMenuCommand) (menu.Menu, error) {
	return s.store.GetMenu(ctx, cmd)
}

func (s *Service) Insert(ctx context.Context, menu menu.Menu) error {
	return s.store.Insert(ctx, menu)
}
func (s *Service) BatchInsert(ctx context.Context, cmd []menu.Menu) (err error) {
	return s.store.BatchInsert(ctx, cmd)
}

func (s *Service) Update(ctx context.Context, cmd menu.Menu) error {
	return s.store.Update(ctx, cmd)
}

func (s *Service) Remove(ctx context.Context, cmd menu.RemoveMenuCommand) error {
	return s.store.Remove(ctx, cmd)
}

// GetMenuWithRole 用户登录后查询页面主菜单功能
func (s *Service) GetMenuWithRole(ctx context.Context, roleIds []string) ([]*menu.MainMenu, error) {
	//menus, err := s.store.GetMenuWithRole(ctx, roleIds)
	//if err != nil {
	//	return nil, err
	//}
	//m := make([]menu.MainMenu, 0)
	//for i := 0; i < len(menus); i++ {
	//	if menus[i].ParentId != 0 {
	//		continue
	//	}
	//	menuInfo := menuCall(menus, menus[i])
	//	m = append(m, menuInfo)
	//}
	//return m, nil
	return s.store.GetMenuWithRoleNew(ctx, roleIds)
}
func (s *Service) UpdateMenuApi(ctx context.Context, cmd menu.UpdateMenuApiCommand) (err error) {
	return s.store.UpdateMenuApi(ctx, cmd)
}

func (s *Service) ExportMenu(ctx context.Context, cmd menu.GetMenuCommand) (result []menu.Menu, err error) {
	items, err := s.store.GetMenuList(ctx)
	if err != nil {
		return nil, err
	}
	//根据id查询菜单信息列表，注意parentid为0的是一级菜单
	result = FindMenuAndDescendants(items, cmd.Id)
	return
}
